public class MyBinarySearchTree {
    class TreeNode{
        int val;
        TreeNode right;
        TreeNode left;
        public TreeNode(int val){
            this.val = val;
        }
    }
    TreeNode root = null;

    public boolean search(int val){
        TreeNode cur = root;
        while(cur != null){
            if(cur.val > val){
                cur = cur.left;
            } else if(cur.val < val){
                cur = cur.right;
            } else {
                return true;
            }
        }
        return false;
    }
    public void insert(int val){
        TreeNode node = new TreeNode(val);
        if(root == null){
            root = node;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null){
            if(cur.val > val){
                parent = cur;
                cur = cur.left;
            } else if(cur.val < val){
                parent = cur;
                cur = cur.right;
            } else {
                return;
            }
        }
        if(parent.val > val){
            parent.left = node;
        } else {
            parent.right = node;
        }
    }
    public boolean delete(int val){
        TreeNode cur = root;
        TreeNode parent = cur;
        while(cur != null){
            if(cur.val > val){
                parent = cur;
                cur = cur.left;
            }else if(cur.val < val){
                parent = cur;
                cur = cur.right;
            } else {
                //以下是删除的逻辑
                remove(parent, cur);
                return true;
            }
        }
        return false;
    }
    private void remove(TreeNode parent, TreeNode cur){
        //cur.left == null && cur.right == null
        if(cur.left == null && cur.right == null){
            if(cur == parent.right){
                parent.right = null;
            } else {
                parent.left = null;
            }
            return;
        }
        //cur.left == null
       if(cur.left == null){
            if(cur == root){
                cur = null;
            } else if(cur == parent.right){
                parent.right = cur.right;
            } else {
                parent.left = cur.right;
            }
        //cur.right == null
        } else if(cur.right == null){
            if(cur == root){
                cur = null;
            } else if(cur == parent.right){
                parent.right = cur.left;
            } else {
                parent.left = cur.left;
            }
        }
        //cur.right != null && cur.left != null
        TreeNode targetParent = cur;
        //在左子树中寻找最大的将cur中的数进行替换
        TreeNode target = cur.left;
        while(target.right != null){
            targetParent = target;
            target = target.right;
        }
        cur.val = target.val;
        //判断target是左子树还是右子树
        if(target == targetParent.right){
            targetParent.right = target.left;
        } else {
            targetParent.left = target.left;
        }
    }
}
